package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DayView extends Activity {
	
	public Calendar day;
	private String[][] about;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.day);
	    day = Calendar.getInstance();
	    onNewIntent(getIntent());
	    
	    updateDayActivities();
	    
	    ListView activities = (ListView)findViewById(R.id.listViewActivitiesDay);
	    activities.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,
	    		int position, long id) {
	    		showActivities(about[position][0], about[position][1],
	    				about[position][2], about[position][3]);
	    	}
	    });
	    
	    TextView title  = (TextView) findViewById(R.id.textView1);
	    title.setText(android.text.format.DateFormat.format("dd MMMM yyyy", day));

 
	    TextView previous  = (TextView) findViewById(R.id.previous);
	    previous.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (day.get(Calendar.DAY_OF_MONTH) == day.getActualMinimum(Calendar.MONTH) ){
					if(day.get(Calendar.MONTH) == day.getActualMinimum(Calendar.MONTH)){
						day.set((day.get(Calendar.YEAR)-1),12,31);
					}else {
						day.set((day.get(Calendar.YEAR)),day.getActualMinimum(Calendar.MONTH)-1,1);
					}
				} else {
					day.set(Calendar.DAY_OF_MONTH,day.get(Calendar.DAY_OF_MONTH)-1);
				}
				refreshCalendar();
			}
		});
	    
	    TextView next  = (TextView) findViewById(R.id.next);
	    next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(day.get(Calendar.MONTH) == day.getActualMaximum(Calendar.MONTH)){
					day.set((day.get(Calendar.YEAR)+1),1,1);
					System.out.println(day.get(Calendar.MONTH));
				}else if (day.get(Calendar.DAY_OF_MONTH) == day.getActualMaximum(Calendar.DAY_OF_MONTH)){
					day.set((day.get(Calendar.YEAR)),(day.get(Calendar.MONTH)+1),1);
				} else {
					day.set(Calendar.DAY_OF_MONTH,day.get(Calendar.DAY_OF_MONTH)+1);
				}
				refreshCalendar();
			}
				
		});
	}
		
	public void onNewIntent(Intent intent) {
		String date = intent.getStringExtra("date");
		String[] dateArr = date.split("-"); // date format is yyyy-mm-dd
		day.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	public void refreshCalendar()
	{
		TextView title  = (TextView) findViewById(R.id.textView1);		
		title.setText(android.text.format.DateFormat.format("dd MMMM yyyy", day));
	}
	
	@Override
	public void onBackPressed() {
		int dayInt = day.get(Calendar.DAY_OF_MONTH);
		int month = day.get(Calendar.MONTH);
        int year = day.get(Calendar.YEAR);
        
	    Intent trocatela = new Intent(DayView.this, CalendarView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+dayInt);

		DayView.this.startActivity(trocatela);
		DayView.this.finish();
	}
	
	private void updateDayActivities(){
        ListView activities = (ListView)findViewById(R.id.listViewActivitiesDay);

	    BD bd = new BD();
	    ArrayList<String> activi = bd.getActivityOrderByHour(this);
	    for (int i = 0; i < activi.size(); i++) {
			System.out.println(activi.get(i));
		}
	    
	    ArrayList<String> classes = bd.getClassesByDay(this, String.valueOf(day.get(Calendar.DAY_OF_WEEK)));

	    int count = 0;
		int dayInt = day.get(Calendar.DAY_OF_MONTH);
		int month = day.get(Calendar.MONTH);
        int year = day.get(Calendar.YEAR);
        
		String date = (dayInt+"-"+month+"-"+year);

		ArrayList<String> activitiesDay = new ArrayList<String>();
		for (int i = 0; i < activi.size(); i++) {
			String[] activity = activi.get(i).split(" ");
			if (date.equals(activity[0])){
				count++;
				activitiesDay.add(activi.get(i));
			}
	    }
		
		System.out.println("Calendar day: " +day.get(Calendar.DAY_OF_WEEK));
		for (String aulas : classes){
		    count++;
		    System.out.println(aulas);
		    activitiesDay.add(aulas);
		}

		about = new String[count][4];;

		for (int i = 0; i < activitiesDay.size(); i++) {
			String[] activity = activitiesDay.get(i).split(" ");
			if (date.equals(activity[0])){
				System.out.println(activity[0]);
			about[i][0] = activity[0];
			System.out.println(activity[1]);
			about[i][1] = activity[1];
			System.out.println(activity[2]);
			about[i][2] = activity[2];
			System.out.println(activity[3]);
			about[i][3] = activity[3];
			}else{
			    if (classes.size()>0){
			        about[i][0] = activity[0];
			        about[i][1] = activity[1];
			        about[i][2] = activity[2];
                    about[i][3] = activity[3];
			    }
			}
		}
	    
	    
	    String[] values = new String[about.length];
	    for (int i = 0; i < values.length; i++) {
			values[i] = about[i][1] + " - " + about[i][2];
		}
	    
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    activities.setAdapter(adapter);
	    activities.invalidate();
	}
	
	private void showActivities(final String date, final String hour, final String name, final String description) {
		 final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(name);
		 helpBuilder.setMessage("Date: " + date + "\nHour: " + hour + "\nName: " + name + "\nDescription: " + description);
		 helpBuilder.setPositiveButton("Editar", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  //editActivityName(); //date, name, description
				  dialog.cancel();
			  }
		 });
		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
			 });
		 helpBuilder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  BD bd = new BD();
				  bd.removeActivity(DayView.this, name);
				  dialog.cancel();
				  startActivity(getIntent()); 
				  finish();
			  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		}

}
